home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 59454 / 59454.xpi / content / SearchEngineWrapper.js < prev   
Text File  |  2010-01-15  |  8KB  |  231 lines

  1.  
  2. BartUniversalSearch.SearchEngineObserver = BartUtils.defineClass(
  3. {
  4.     name: "UniversalSearchSearchEngineObserver",
  5.     construct: function()
  6.     {
  7.         this.register();
  8.     },
  9.  
  10.     methods:
  11.     {
  12.         observe: function(subject, topic, verb)
  13.         {
  14.             if(topic == "browser-search-engine-modified")
  15.             {
  16.                 switch(verb)
  17.                 {
  18.                     case "engine-changed":
  19.                     case "engine-added":
  20.                     case "engine-removed":
  21.                         BartUniversalSearch.SearchEngineWrapper._buildSearchEngineList();
  22.                         break;
  23.                 }
  24.             }
  25.         },
  26.  
  27.         register: function()
  28.         {
  29.             var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  30.             observerService.addObserver(this, "browser-search-engine-modified", false);
  31.         },
  32.  
  33.         unregister: function()
  34.         {
  35.             var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  36.             observerService.removeObserver(this, "browser-search-engine-modified");
  37.         }
  38.     }
  39. });
  40.  
  41. BartUniversalSearch.SearchEngineWrapper = BartUtils.defineClass(
  42. {
  43.     name: "UniversalSearchEngineWrapper",
  44.     construct: function(engine)
  45.     {
  46.         this.engine = engine;
  47.     },
  48.  
  49.     methods:
  50.     {
  51.         getName: function()
  52.         {
  53.             return this.engine.name;
  54.         },
  55.  
  56.         /**
  57.          * Get Icon url of the engine. May be null.
  58.          */
  59.         getIconUrl: function(defaultUrl)
  60.         {
  61.             var result = this.engine.iconURI ? this.engine.iconURI.spec : null;
  62.             if(!result)
  63.             {
  64.                 result = (defaultUrl == undefined? null : defaultUrl);
  65.             }
  66.  
  67.             return result;
  68.         },
  69.  
  70.         /**
  71.          * Get a search url for a search term
  72.          */
  73.         getSearchUrl: function(searchTerm)
  74.         {
  75.             var url = this.engine.getSubmission(searchTerm, null).uri.spec;
  76.  
  77.             if(url.search(/ebay.com\//i) != -1)
  78.             {
  79.                 url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?type=4&campid=5336506940&toolid=10001&mpre="
  80.                     + encodeURIComponent("http://search.ebay.com/"+searchTerm);
  81.             }
  82.             else if(url.search(/amazon.com\//i) != -1)
  83.             {
  84.             
  85.                 url = "http://www.amazon.com/s/?url=search-alias%3Daps&field-keywords=" + searchTerm + "&tag=ff-ex-us-us-20"
  86.             }
  87.  
  88.             return url;
  89.         }
  90.     },
  91.  
  92.     statics:
  93.     {
  94.         _removeFromString: function(str, remove)
  95.         {
  96.             //var pos = str.indexOf(remove);
  97.             var pos = str.search(new RegExp(remove, "i"));
  98.             if(pos != -1)
  99.             {
  100.                 var pos2 = str.indexOf("&", pos);
  101.                 if(pos2 != -1)
  102.                 {
  103.                     str = str.substring(0, pos) + str.substring(pos2 + 1);
  104.                 }
  105.                 else
  106.                 {
  107.                     str = str.substring(0, pos);
  108.                 }
  109.             }
  110.  
  111.             return str;
  112.         },
  113.  
  114.         getAvailableSearchEngines: function()
  115.         {
  116.             return BartUniversalSearch.SearchEngineWrapper.availableEngines;
  117.         },
  118.  
  119.         /**
  120.          * Get engine by name. Return null if not found.
  121.          */
  122.         getEngine: function(name, useDefaultIfNotFound)
  123.         {
  124.             var engines = BartUniversalSearch.SearchEngineWrapper.getAvailableSearchEngines();
  125.             if(engines)
  126.             {
  127.                 for(var i = 0; i < engines.length; i++)
  128.                 {
  129.                     if(engines[i].getName() == name)
  130.                         return engines[i];
  131.                 }
  132.             }
  133.  
  134.             return useDefaultIfNotFound ? BartUniversalSearch.SearchEngineWrapper.getDefaultEngine() : null;
  135.         },
  136.  
  137.         _buildSearchEngineList: function()
  138.         {
  139.             var searchService = Components.classes["@mozilla.org/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService);
  140.             var engines = searchService.getVisibleEngines({ });
  141.  
  142.             var result = [];
  143.  
  144.             if(engines.length <= 0)
  145.             {
  146.                 // build our engines here
  147.                 BartUniversalSearch.SearchEngineWrapper._generateCustomEngines(result);
  148.             }
  149.             else
  150.             {
  151.                 for (var i = 0; i < engines.length; i++)
  152.                 {
  153.                     result.push(new BartUniversalSearch.SearchEngineWrapper(engines[i]));
  154.                 }
  155.             }
  156.  
  157.             BartUniversalSearch.SearchEngineWrapper.availableEngines = result;
  158.         },
  159.  
  160.         init: function()
  161.         {
  162.             BartUniversalSearch.SearchEngineWrapper._buildSearchEngineList();
  163.  
  164.             BartUniversalSearch.SearchEngineWrapper.engineObserver = new BartUniversalSearch.SearchEngineObserver();
  165.         },
  166.  
  167.         uninit: function()
  168.         {
  169.             if(BartUniversalSearch.SearchEngineWrapper.engineObserver)
  170.             {
  171.                 BartUniversalSearch.SearchEngineWrapper.engineObserver.unregister();
  172.                 BartUniversalSearch.SearchEngineWrapper.engineObserver = null;
  173.             }
  174.  
  175.             if(BartUniversalSearch.SearchEngineWrapper.availableEngines)
  176.             {
  177.                 BartUniversalSearch.SearchEngineWrapper.availableEngines.splice(0);
  178.             }
  179.         },
  180.  
  181.         /**
  182.          * Create custom engines and save them into engines array.
  183.          */
  184.         _generateCustomEngines: function(engines)
  185.         {
  186.             var customEngine =
  187.                 {
  188.                     getName: function() {return "Google";},
  189.                     getIconUrl: function(defaultUrl) {return defaultUrl;},
  190.                     getSearchUrl: function(searchTerm)
  191.                                     {
  192.                                         var searchParam = BartUtils.UTF8Encode(searchTerm);
  193.                                         searchParam = searchParam.replace(/%20/g, "+");
  194.                                         return "http://www.google.com/search?q=" + searchParam;
  195.                                     }
  196.                 };
  197.  
  198.             engines.push(customEngine);
  199.         },
  200.  
  201.         /**
  202.          * Get the defult search engine. init() must be called before using this method.
  203.          * A valid engine will be returned.
  204.          */
  205.         getDefaultEngine: function()
  206.         {
  207.             var searchService = Components.classes["@mozilla.org/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService);
  208.             // first we use browser's current engine
  209.             var engine = searchService.currentEngine;
  210.             if(!engine)
  211.             {
  212.                 // if current engine is empty, use browser default engine
  213.                 engine = searchService.defaultEngine;
  214.             }
  215.  
  216.             if(!engine)
  217.             {
  218.                 // if both current engine and default engine are unavailable, use the first available engine
  219.                 var availableEngines = BartUniversalSearch.SearchEngineWrapper.getAvailableSearchEngines();
  220.                 // init() method makes sure that there are always avaliable engines.
  221.                 engine = availableEngines[0];
  222.             }
  223.             else
  224.             {
  225.                 engine = new BartUniversalSearch.SearchEngineWrapper(engine);
  226.             }
  227.  
  228.             return engine;
  229.         }
  230.     }
  231. });